import AjaxTool from './ajaxTool.js'
const {ajax} = AjaxTool


let toBook = document.querySelector('#toBook')
let tip = document.querySelector('#tip')
let mask = document.querySelector('#mask')
let tipClose = document.querySelector('#tipClose')
let username = document.querySelector('#username')
let phone = document.querySelector('#phone')
let idCard = document.querySelector('#idcard')
let myBook = document.querySelector('#myBook')

const baseUrl = 'http://shopback.bluej.cn/api/'
const usernameReg = /^[\w(\u4e00-\u9fa5)]{3,15}$/g
const phoneReg = /^(13[0-9]|14[01456879]|15[0-3,5-9]|16[2567]|17[0-8]|18[0-9]|19[0-3,5-9])\d{8}$/g
const idCardReg = /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/g
// /^1(3([0-35-9]\\d|4[1-8])|4[14-9]\\d|5([0125689]\\d|7[1-79])|66\\d|7[2-35-8]\\d|8\\d{2}|9[89]\\d)\\d{7}$/


let timer = null

let usernameVal 
let phoneVal
let idCardVal 
toBook.onclick = function(){
    let  url = `${baseUrl}f_user/register`
    usernameVal = username.value
    phoneVal = phone.value
    idCardVal = idCard.value
    let usernamePass = testMes(usernameVal, username, usernameReg)
    let phonePass = testMes(phoneVal, phone, phoneReg)
    let idCardPass= testMes(idCardVal, idCard, idCardReg)
    let userMes = {
        phone:phoneVal,
        'name':usernameVal,
        card:idCardVal
    }

    if(!usernameVal||!phoneVal||!idCardVal){
        showTip()
        setTimeout(()=>{
            hideTip()
        },3000)
        return
    }else if(usernamePass&&phonePass&&idCardPass){
        ajax("POST",url,JSON.stringify(userMes),'application/json').then(res=>{
            // console.log(res);
            let username = document.querySelector('#username')
            let phone = document.querySelector('#phone')
            let idCard = document.querySelector('#idcard')
            let data = JSON.parse(res)
            const {msg} = data
            if(msg == '登录成功'){
                const {result} = data
                localStorage.setItem('token',result.token)
                delete userMes.card
                let userInfo = JSON.stringify(userMes) 
                localStorage.setItem('userinfo',userInfo)
                username.value= ''
                phone.value = ''
                idCard.value = ''
                location.href = './html/list.html'
            }
        })
    }


}
myBook.addEventListener('click',function(){

    usernameVal = username.value
    phoneVal = phone.value
    
    // console.log(usernameVal,phoneVal,idCardVal);
    if(!usernameVal||!phoneVal){
        showTip()

        timer = setTimeout(()=>{
            hideTip()
        },3000)
        return
    }
    location.href  = `./html/myBook.html?phone=${phoneVal}&name=${usernameVal}`

})

function showTip(message) {
    if(message){
        tip.children[1].innerText = message
    }
    clearTimeout(timer)
    tip.classList.add('showtip')
    mask.classList.add('showtip')
}
function hideTip(params) {
    tip.classList.remove('showtip')
    mask.classList.remove('showtip')
}


tipClose.onclick = function(){
    hideTip()
}

function showSuccess(dom, mes) {
    dom.nextElementSibling.style.color = '#d7fbe8'
    dom.nextElementSibling.innerHTML = mes
    return true
}

function showFail(dom, mes) {
    dom.nextElementSibling.style.color = 'red'
    dom.nextElementSibling.innerHTML = mes
}

function testMes(val, dom, reg) {
    let res = val.match(reg)
    let tipName = dom.placeholder.replace('请输入', '')
    if (val === '') {
        return showFail(dom, `请输入${tipName}`)
    }
    if (res) {
        showSuccess(dom, `输入${tipName}格式正确`)
        return true
    } else {
        showFail(dom, `输入${tipName}格式错误`)
    }
}